package com.rigsofrods.truckeditor.gui.editor;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;

public class SectionEditorPanel extends JPanel implements TreeSelectionListener, FocusListener {

	private static final long serialVersionUID = 1408538650170437668L;
	private Object constr = BorderLayout.CENTER;
	private JPanel empty;
	
	public SectionEditorPanel() {
		super(new BorderLayout());
		empty = new JPanel();		
		add(empty, constr);
	}

	@Override
	public void valueChanged(TreeSelectionEvent e) {
		JTree tree = (JTree) e.getSource();
		//System.out.println("Selection changed");
		if(tree.getSelectionCount() > 0){
			TreePath[] paths = tree.getSelectionPaths();
			showEditor(SectionEditorFactory.getEditor(paths));
		}
		else {
			showEditor(empty);
		}
	}

	private void showEditor(JComponent c){
		removeAll();
		if(c != null)
			add(c, constr);			
		else
			add(empty, constr);
		revalidate();
	}
	
	@Override
	public void focusGained(FocusEvent e) {
		//Logger.getLogger("gui.editor.SectionEditorPanel");
		//System.out.println("gui.editor.SectionEditorPanel.focusGained() by: "+e);
	}

	@Override
	public void focusLost(FocusEvent e) {
		// TODO Auto-generated method stub
		//System.out.println("gui.editor.SectionEditorPanel.focusLost() by: "+e);
	}
}
